import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.CachingRecommender;
import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.AveragingPreferenceInferrer;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;

public class ItemRecommender {
	public static void main(String[] args) throws TasteException, IOException {
		DataModel model;
		model = new FileDataModel(new File(args[0]));
		ItemSimilarity itemSimilarity = new PearsonCorrelationSimilarity(model);
		Recommender recommender = new GenericItemBasedRecommender(model,
				itemSimilarity);
		Recommender cachingRecommender = new CachingRecommender(recommender);
		Long userid = null;
		Long itemid = null;
		Integer actualValue = null;
		double prefScore = 0;

		File file = new File(args[1]);
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String s = null;
		do {
			s = reader.readLine();
			s = s.trim();
			String[] parts = s.split(",");
			if (s == null)
				break;
			userid = Long.parseLong(parts[0]);
			itemid = Long.parseLong(parts[1]);
			actualValue = Integer.parseInt(parts[2]);
			prefScore = cachingRecommender.estimatePreference(userid, itemid);
			System.out.println(userid + " " + itemid + " " + actualValue + " "
					+ prefScore);
		} while (s != null);
		reader.close();
	}

}
